

var user= {

    login:function (username, password) {
        _common_util.request({
            method : 'POST',
            url : "http://localhost:8080/admin/login",
            data: {
                'username' : username,
                'password' : password
            },
            success : function(res){
                //如果点remember me 存localStorage
                if ($('#rememberme').prop("checked")){
                    window.localStorage.setItem('islogin',username);
                }
                sessionStorage.setItem('islogin',username);
                window.location.href = './home.html';
                console.log(res);
            },
            error : function(err){
                $('#login-error').css('display','inline-block').html(err);

                console.log(err);
            }
        })
    },
    check_vaild : function(){
        _common_util.request({

        })
    },
    // get_account:function(){
    //     _common_util.request({
    //         method:'GET',
    //         url:"http://localhost:8080/admin/get_account",
    //         success:function (res) {
    //             console.log("get_account:"+res);
    //         }
    //     })
    // },
    register:function (username,password,phonenumber,email,question,answer) {
        _common_util.request({
            method: 'POST',
            url: "http://localhost:8080/admin/register",
            data: {
                'username'  : username,
                'password'  : password,
                'phoneNumber':phonenumber,
                'email'     :    email,
                'question'  : question,
                'answer'    :   answer
            },
            success:function (res) {
                $('#login-username').val(username);
                $('#login-password').val(password);
                window.localStorage.setItem('answer',answer);
                window.wxc.xcConfirm('注册成功','success',{onOk:function () {
                        $('body').animate( {scrollTop: 0}, 500);
                    }})
                $('#reg-success').css('display','inline-block');
                console.log(res);
            },
            error:function (err) {
                if (err == '用户名已存在'){
                    $('#username-error').css('display','inline-block').html('用户名已存在')
                }else if (err == '电话号码已注册'){
                    $('#phone-error').css('display','inline-block').html('电话号码已注册')
                }else if (err =='邮箱已注册'){
                    $('#email-error').css('display','inline-block').html('邮箱已注册')
                }
                console.log(err);
            }
        })
    },
    getaccountinfo:function () {

        let username = localStorage.getItem('islogin');
        console.log("islogin::"+username);
        $.ajax({
            type        :  'get',
            url         :  "http://localhost:8080/admin/get_account",
            dataType    :  'json',

            success     : function(res){
                console.log(res)
                if( 0 === res.status) {
                    console.log("成功取得");
                    window.localStorage.setItem('username',res.data.username);
                    $('#user-nickname').val(res.data.username);
                    $('#user-email').val(res.data.email);
                    $('#user-phone').val(res.data.phoneNumber);
                    $('#question').val(res.data.question);
                    $('#user-detail-address').val(res.data.address);
                    if (res.data.role === 1){
                        $('.my-product').css('display','none');
                        $('#user-realname').val('尚未实名认证')
                    }else{
                        $('.my-product').css('display','block');
                        $('#user-realname').val('已完成实名认证')
                    }

                }

            },

        });
    },
    getQuestionByUsername:function(username){
        $.ajax({
            type        :  'POST',
            url         :  "http://localhost:8080/admin/forget_password",
            dataType    :  'json',
            async       :   false,
            data        : {
                'type':'username',
                'value': username
            },
            success     :function (res) {
                if (0 === res.status){
                    window.localStorage.setItem('fusername',username);
                    window.localStorage.setItem('fquestion',res.data);
                    console.log('question:'+res.data);
                }else {
                    console.log('user.js'+'用户名不存在');
                }
            }
        })
    },
    // getaccountinfotest:function(username){
    //     _common_util.request({
    //         method  : 'GET',
    //         url     : 'http://localhost:8080/admin/get_account',
    //         data    :{
    //             'username':username
    //         },
    //         success : function (res) {
    //             console.log("common-util测试："+res);
    //         }
    //     })
    // },

    userexit :function () {
        $.ajax({
            type:'POST',
            url: _common_util.getServerURL('admin/logout'),
            success:function (res) {

                if (res.status === 0){
                    window.localStorage.clear();
                    sessionStorage.clear();
                    window.location.href='./my-account.html';
                }

            }
        })

    },


    isLogin:function () {
        if (window.localStorage.getItem('islogin') !=null || sessionStorage.getItem('islogin') !=null  ){
            return true;
        }else {
            return false;
        }
    }
}

